<template>
  <div>加载中...</div>
</template>

<script>
//获取url后面的参数
function getQueryString(name) {
  const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  const urlObj = window.location;
  var r =
    urlObj.href.indexOf("#") > -1
      ? urlObj.hash.split("?")[1].match(reg)
      : urlObj.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}
export default {
  data() {
    return {
      //code
      code: {
        code: "",
      },
      //token
      token: {
        appid: "produce@mstar.cn",
        secert: "c811385fe41607e64d816db509f33ffb",
        timestamp: "",
      },
    };
  },
  created() {
    this.getToken();
    this.jurisdiction();
  },
  methods: {
    //判断是否有权限进入页面
    async jurisdiction() {
      this.code.code = getQueryString("code");

      const { data: res } = await this.$http.get("weixin/show.php", {
        params: this.code,
      });
      console.log(res.data);
      //如果checkFlag为0没有登陆权限直接跳转到登陆页面
      if (res.data.checkFlag == 0) {
        this.$message.error("你没有权限登陆");
        return this.$router.push("/login");
      }
      //把信息保存在sessionstorage
      window.sessionStorage.setItem("userFaceLink", res.data.userFaceLink);
      window.sessionStorage.setItem("userName", res.data.userName);
      window.sessionStorage.setItem("checkFlag", res.data.checkFlag);
      window.sessionStorage.setItem("userCheckFlag", res.data.userCheckFlag);
      return this.$router.push("/home");
    },
    //获取token
    async getToken() {
      var time = Date.parse(new Date()).toString();
      this.token.timestamp = time.substr(0, 10);
      //console.log(time);
      const { data: res } = await this.$http.post(
        "api/get_token.php",
        this.token
      );
      //把token保存在sessionstorage;
      window.sessionStorage.setItem("token", res.data.token);

      console.log(res.data.token);
    },
  },
};
</script>

<style>
</style>